* Construct the Indonesian dataset
clear

* Import data
// 1994
import delimited "input/IND94A.tab"
sort psid
save "temp/94atemp.dta", replace
import delimited "input/IND94B.tab", clear
sort psid
merge psid using "temp/94atemp.dta"
drop _merge
save "temp/INDUS94.dta", replace
// 1995
import delimited "input/IND95A.tab", clear
sort nomor
save "temp/95atemp.dta", replace
import delimited "input/IND95B.tab", clear
sort nomor
merge nomor using "temp/95atemp.dta"
drop _merge
save "temp/INDUS95.dta", replace
// 1996
import delimited "input/IND96A.tab", clear
sort no_prsh
save "temp/96atemp.dta", replace
import delimited "input/IND96B.tab", clear
sort no_prsh
merge no_prsh using "temp/96atemp.dta"
drop _merge
save "temp/INDUS96.dta", replace
// 1997
import delimited "input/IND97A.tab", clear
sort no_prsh
save "temp/97atemp.dta", replace
import delimited "input/IND97B.tab", clear
sort no_prsh
merge no_prsh using "temp/97atemp.dta"
drop _merge
save "temp/INDUS97.dta", replace
// 2000
import delimited "input/INDUS00.tab", clear
save "temp/INDUS00.dta", replace // this is year 2000
// 1990-1993, 1998-1999
foreach t of numlist 90 91 92 93 98 99 {
import delimited "input/INDUS`t'.tab", clear
save "temp/INDUS`t'.dta", replace
}

* Select variables of interest
foreach t of num 90 91 92 93 94 95 97 98 {
use "temp/INDUS`t'.dta", clear
if `t' < 94 { 
keep 	psid ///
		vtlvcu`t'	///
		output`t'	///
		prod5d`t'	///
		dsrvyr`t'	///
		rdnvcu`t'    ///
		rimvcu`t'    ///
		prprex`t'	///
		rtlvcu`t' ///
		zpskcu`t' ///
		zpsccu`t' ///
		znskcu`t' /// 
		znsccu`t' 

rename	zpskcu`t'	wagesprodkind
rename	zpsccu`t'	wagesprodcash
rename	znskcu`t'	wagesemplkind
rename	znsccu`t'	wagesemplcash

gen wages = wagesprodcash + wagesprodkind + wagesemplcash + wagesemplkind
drop wagesprodcash wagesprodkind wagesemplcash wagesemplkind
}
if `t' >= 94 { 
keep 	psid ///
		vtlvcu`t'	///
		output`t'	///
		prod5d`t'	///
		dsrvyr`t'	///
		rdnvcu`t'    ///
		rimvcu`t'    ///
		prprex`t'	///
		rtlvcu`t' ///
		zpsvcu`t' ///
		znsvcu`t' 
		
rename	zpsvcu`t'	wagesprod // Production workers total (Cash&Kind) 
rename	znsvcu`t'	wagesempl // Other workers total (Cash&Kind) 
gen wages = wagesprod+wagesempl
drop wagesprod wagesempl
}
rename	vtlvcu`t'	va
rename	output`t'	sales
rename	prod5d`t'	mainproduct
rename	dsrvyr`t'	year
rename 	rdnvcu`t'	dommaterials
rename 	rimvcu`t'	foreignmaterial
rename	prprex`t'	exportshare
rename	rtlvcu`t'	materials

label var	psid	"Firm ID"
label var	va	"Value Added"
label var	sales	"Revenue"
label var	mainproduct	"Main Product"
label var	year	"Year of Survey"
label var	dommaterials	"Value of Domestic Materials"
label var	foreignmaterial	"Value of Imported Materials"
label var	exportshare	"Share of Production Exported"
label var	materials	"Value of Total Materials"
	
save "temp/INDUS`t'small.dta", replace		
}
// 1996
use "temp/INDUS96.dta", clear

keep 	psid ///
		vtlvcu96	///
		output96	///
		prod5d96	///
		dsrvyr96	///
		rdnvcu96    ///
		rimvcu96    ///
		prprex96	///
		rtlvcu96 ///
		zpsvcu96 ///
		znsvcu96
		

rename	vtlvcu96	va
rename	output96	sales
rename	prod5d96	mainproduct
rename	dsrvyr96	year
rename 	rdnvcu96	dommaterials
rename 	rimvcu96	foreignmaterial
rename	prprex96	exportshare
rename	rtlvcu96	materials
rename	zpsvcu96	wagesprod
rename	znsvcu96	wagesempl

label var	psid	"Firm ID"
label var	va	"Value Added"
label var	sales	"Revenue"
label var	mainproduct	"Main Product"
label var	year	"Year of Survey"
label var	dommaterials	"Value of Domestic Materials"
label var	foreignmaterial	"Value of Imported Materials"
label var	exportshare	"Share of Production Exported"
label var	materials	"Value of Total Materials"
gen wages = wagesprod+wagesempl
drop wagesprod wagesempl
save "temp/INDUS96small.dta", replace		

// 1999-2000
local years 99 00

foreach x of local years {
use "temp/INDUS`x'.dta", clear
keep 	psid 	vtlvcu`x' output`x' dsrvyr`x'	rdnvcu`x' rimvcu`x' prprex`x' rtlvcu`x' zpsvcu`t' znsvcu`t' 
rename	vtlvcu`x'	va
rename	output`x'	sales
rename	dsrvyr`x'	year
rename 	rdnvcu`x'	dommaterials
rename 	rimvcu`x'	foreignmaterial
rename	prprex`x'	exportshare
rename	rtlvcu`x'	materials
rename	zpsvcu`t'	wagesprod
rename	znsvcu`t'	wagesempl
label var	psid	"Firm ID"
label var	va	"Value Added"
label var	sales	"Revenue"
label var	year	"Year of Survey"
label var	dommaterials	"Value of Domestic Materials"
label var	foreignmaterial	"Value of Imported Materials"
label var	exportshare	"Share of Production Exported"
label var	materials	"Value of Total Materials"
replace year=19`x'
gen wages = wagesprod+wagesempl
drop wagesprod wagesempl
save "temp/INDUS`x'small.dta", replace	
}

* Pool all years together
use "temp/INDUS00Small.dta", clear
append using "temp/INDUS99Small.dta"
foreach t of num 90(1)98 {
append using "temp/INDUS`t'Small.dta"
}

replace year=1990 if year==90
replace year=1991 if year==91
replace year=1992 if year==92
replace year=1993 if year==93
replace year=1994 if year==94
replace year=1995 if year==95
replace year=1996 if year==96
replace year=1997 if year==97
replace year=1998 if year==98
replace year=1999 if year==99
replace year=2000 if year==1900
rename psid id
sort year

do "dofiles/ConstructNewVariables_Indo.do"
sort year id
save "temp/SI.dta", replace
